package com.letv.whatslive.model.convert;

import com.google.common.collect.Lists;
import com.letv.whatslive.model.Questionnaire;
import com.letv.whatslive.model.SubQuestionnaire;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import java.util.List;

/**
 * Created by shenyue on 16-4-5.
 */
public class SubQuestionnaireConvert {


    public static DBObject castSubQuestionnaireToDBObject(SubQuestionnaire subQuestionnaire) {
        DBObject dbo = new BasicDBObject();
        if (subQuestionnaire.getId() != null) {
            dbo.put("_id", subQuestionnaire.getId());
        }
        if (subQuestionnaire.getType() != null)
            dbo.put("type", ObjUtils.toInteger(subQuestionnaire.getType()));
        if (subQuestionnaire.getIsRequired() != null)
            dbo.put("isRequired", ObjUtils.toInteger(subQuestionnaire.getIsRequired()));
        if (subQuestionnaire.getPriority() != null)
            dbo.put("priority", ObjUtils.toLong(subQuestionnaire.getPriority()));
        dbo.put("name", ObjUtils.ifNull(subQuestionnaire.getName(), ""));
        if (subQuestionnaire.getOptions() != null) {
            dbo.put("options", subQuestionnaire.getOptions());
        }
        return dbo;
    }

    public static SubQuestionnaire castDBObjectToSubQuestionnaire(DBObject dbObject) {
        SubQuestionnaire subQuestionnaire = new SubQuestionnaire();
        if (dbObject != null) {
            subQuestionnaire.setId(ObjUtils.toLong(dbObject.get("_id")));
            subQuestionnaire.setName(ObjUtils.toString(dbObject.get("name")));
            subQuestionnaire.setType(ObjUtils.toInteger(dbObject.get("type")));
            subQuestionnaire.setIsRequired(ObjUtils.toInteger(dbObject.get("isRequired")));
            subQuestionnaire.setPriority(ObjUtils.toLong(dbObject.get("priority")));
            if (dbObject.get("options") != null) {
                if (dbObject.get("options") instanceof BasicDBList) {
                    BasicDBList dbList = (BasicDBList) (dbObject.get("options"));
                    if (dbList.size() > 0) {
                        List<String> options = Lists.newArrayList();
                        for (int i = 0; i < dbList.size(); i++) {
                            if (dbList.get(i) instanceof String) {
                                options.add((String) (dbList.get(i)));
                            } else {
                                throw new ClassCastException("options can not cast to String!");
                            }
                        }
                        subQuestionnaire.setOptions(options);
                    }
                } else if (dbObject.get("options") instanceof BasicDBObject && ((BasicDBObject) dbObject.get("options")).size() > 0) {
                    List<String> options = Lists.newArrayList();
                    String option = ((String) (dbObject.get("options")));
                    options.add(option);
                    subQuestionnaire.setOptions(options);
                }

            }
        } else {
            return null;
        }
        return subQuestionnaire;
    }
}
